/**==================================================================
 * Copyright: Belongs to Cystc (org.cystc) 2013~2065
 * FileName: org.cystc.users.action.UserAction.java
 * Description: 
 * Changes: 
 * Date                Author           Note
 * ===================================================================
 * 2013-3-11    Cysct(cystc@126.com)     Create file,implement function
**===================================================================*/
package org.cystc.users.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.cystc.users.model.User;
import org.cystc.users.services.inter.UserServices;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

public class UserAction implements Preparable{
	 private static final Logger LOGGER = Logger.getLogger(UserAction.class.getName());
	 
	    private List<?> users;
	 
	    private Integer id;
	    private String username;
	    private String password;
	 
	    @Autowired
	    private UserServices userServices;
	 
	    public UserServices getUserServices() {
	        return userServices;
	    }
	 
	    public void setUserServices(UserServices userServices) {
	        this.userServices = userServices;
	    }
	 
	    public List<?> getUsers() {
	        return users;
	    }
	 
	    public void setUsers(List<?> users) {
	        this.users = users;
	    }
	 
	    public Integer getId() {
	        return id;
	    }
	 
	    public void setId(Integer id) {
	        this.id = id;
	    }
	 
	    public String getUsername() {
	        return username;
	    }
	 
	    public void setUsername(String username) {
	        this.username = username;
	    }
	 
	    public String getPassword() {
	        return password;
	    }
	 
	    public void setPassword(String password) {
	        this.password = password;
	    }
	 
	    public String listAll() {
	        if (userServices == null) {
	            return Action.ERROR;
	        }
	        LOGGER.debug("Get all users");
	        users = userServices.findAll();
	        LOGGER.debug("Users number = " + users.size());
	        return Action.SUCCESS;
	    }
	 
	    public String delete() {
	        if (userServices == null) {
	            return Action.ERROR;
	        }
	 
	        User user = getUser(id);
	        if (user == null) {
	            return Action.ERROR;
	        }
	 
	        LOGGER.debug("Delete user " + user.getUsername() + " with id " + user.getId());
	        userServices.delete(user);
	        return Action.SUCCESS;
	    }
	 
	    public String add() {
	        if (userServices == null) {
	            return Action.ERROR;
	        }
	 
	        User user = new User();
	        user.setUsername(username);
	        user.setPassword(password);
	        LOGGER.debug("Add user: " + user.getUsername());
	        userServices.add(user);
	        return Action.SUCCESS;
	    }
	 
	    public String execute() {
	        return Action.SUCCESS;
	    }
	 
	    public void prepare() throws Exception {
	 
	    }
	 
	    private User getUser(Integer id) {
	        LOGGER.debug("Get user with id = " + id);
	        if (id != null) {
	            List<?> users = userServices.findById(id);
	            LOGGER.debug("Number of users with id = " + id + ": " + users.size());
	            if (users.size() == 1) {
	                return (User) users.get(0);
	            }
	        }
	        return null;
	    }
}
